package com.xnyzc.lhy.mis.service.impl.cms.withdrawalrules;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xnyzc.lhy.common.entity.Qw;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EAgreementType;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.cms.agreement.AgreementParam;
import com.xnyzc.lhy.mis.entity.result.withdrawalrules.CmsWithdrawalRules;
import com.xnyzc.lhy.mis.entity.system.OaSysAgreement;
import com.xnyzc.lhy.mis.mapper.system.OaSysAgreementMapper;
import com.xnyzc.lhy.mis.service.cms.withdrawalrules.CmsWithdrawalRulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhaolijie
 * @date
 */
@Service
public class CmsWithdrawalRulesServiceImpl implements CmsWithdrawalRulesService {

    @Autowired
    private OaSysAgreementMapper oaSysAgreementMapper;

    /**
     * 获取提现规则列表
     *
     * @return
     */
    @Override
    public Rv getWithdrawalRulesList() {
        List<Integer> typeList = new ArrayList<>();
        typeList.add(3);
        List<CmsWithdrawalRules> oaSysAgreements = oaSysAgreementMapper.getAgreementContent(typeList);
        if (!oaSysAgreements.isEmpty() && !CollectionUtils.isEmpty(oaSysAgreements)) {
            return Rv.wrap(EErrorCode.success, oaSysAgreements);
        } else {
            return Rv.wrap(EErrorCode.noData, EErrorCode.noData.getDesc());
        }
    }

    /**
     * 获取帮助中心列表
     *
     * @return
     */
    @Override
    public Rv getHelpCenter() {
        Qw qw = Qw.create();
        qw.eq(OaSysAgreement.TYPE, EAgreementType.HELPER_CENTER.getValue());
        qw.eq(OaSysAgreement.DELETED, 0);
        qw.orderByAsc(OaSysAgreement.UPDATE_TIME);

        List<CmsWithdrawalRules> list = oaSysAgreementMapper.selectList(qw);
        return Rv.wrap(list);
    }

    /**
     * 获取帮助详情信息
     *
     * @param parentId：父级编号
     * @return
     */
    @Override
    public Rv getHelpCenterDetail(String parentId) {
        Qw qw = Qw.create();
        qw.eq(OaSysAgreement.TYPE, EAgreementType.HELPER_CENTER.getValue());
        qw.eq(OaSysAgreement.DELETED, 0);
        qw.orderByAsc(OaSysAgreement.UPDATE_TIME);

        List<CmsWithdrawalRules> list = oaSysAgreementMapper.selectList(qw);
        return Rv.wrap(EErrorCode.success, list);
    }

    @Override
    public Rv updateHelpCenterDetail(AgreementParam param) {
        Integer type = EAgreementType.HELPER_CENTER.getValue();
        Long userId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        Date date = new Date();

        Long agreementId = param.getAgreementId();
        String title = param.getTitle();
        String content = param.getContent();
        Integer deleted = param.getDeleted();

        OaSysAgreement agreement = null;
        if (agreementId != null) {
            agreement = oaSysAgreementMapper.selectById(agreementId);
        }
        if (agreement == null) {
            //添加
            agreement = new OaSysAgreement();
            agreement.setType(type);
            agreement.setTitle(title);
            agreement.setContent(content);
            agreement.setCreateUser(userId);
            agreement.setUpdateUser(userId);
            agreement.setCreateTime(date);
            agreement.setUpdateTime(date);
            agreement.setTag(tag);
            if (deleted == null) {
                agreement.setDeleted(1);
            } else {
                agreement.setDeleted(deleted);
            }
            int res = oaSysAgreementMapper.insert(agreement);
            if (res > 0) {
                return Rv.wrap(EErrorCode.updateWalletSuccess);
            }
            return Rv.error(EErrorCode.applyAmountOperateFailure);
        } else {
            //修改
            agreement.setType(type);
            agreement.setTitle(title);
            agreement.setContent(content);
            agreement.setUpdateUser(userId);
            agreement.setUpdateTime(date);
            if (deleted == null) {
                agreement.setDeleted(1);
            } else {
                agreement.setDeleted(deleted);
            }
            int res = oaSysAgreementMapper.updateById(agreement);
            if (res > 0) {
                return Rv.wrap(EErrorCode.updateSuccess);
            }
            return Rv.error(EErrorCode.updateWalletFailure);
        }
    }

    @Override
    public Rv getHelpCenterById(Integer id) {
        if (id == null) {
            throw new PangException(EErrorCode.noData);
        }
        OaSysAgreement agreement = oaSysAgreementMapper.selectById(id);
        if (agreement == null) {
            throw new PangException(EErrorCode.noData);
        }
        return Rv.wrap(agreement);
    }
}
